﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MyWcfRestLib.Files
{
    // TODO: Copy and Move operation could use one same base class "TwoPathOperator", and delete/create could use "OnePathOperator"
    class MoveFileOperator : FileOperator
    {
        public MoveFileOperator(TwoPathOpParams theParams)
            : base(theParams)
        {
            FromFullPath = Path.Combine(DeviceRootFullPath, theParams.FromPath);
            ToFullPath = Path.Combine(DeviceRootFullPath, theParams.ToPath);
        }

        public string FromFullPath { get; private set; }

        public string ToFullPath { get; private set; }

        public bool IsSourceExist { get { return UtilityFunctions.PathExists(FromFullPath); } }

        public bool IsTargetExist { get { return UtilityFunctions.PathExists(ToFullPath); } }

        public bool IsDir
        {
            get { return UtilityFunctions.IsDir(FromFullPath); }
        }

        override public bool CanPerform
        {
            get
            {
                if (0 == string.Compare(FromFullPath, ToFullPath, true))
                    return false;

                bool yesICan = IsSourceExist && (!IsTargetExist);

                return yesICan;
            }
        }

        override public bool PerformOperation()
        {
            string targetParent = Path.GetDirectoryName(ToFullPath);
            Directory.CreateDirectory(targetParent);
            if (IsDir)
            {
                Directory.Move(FromFullPath, ToFullPath);
            }
            else
            {
                File.Move(FromFullPath, ToFullPath);
            }
            return true;
        }
    }
}
